package day15

import (
	"errors"
	"strings"
)

/*
	分割字符串
	example: "abcde", "b"
*/
func Split(src string, sep string) ([]string, error) {
	if sep == "" {
		return nil, errors.New("分割字符串不能为空")
	}
	// var res []string
	var res = make([]string, 0, strings.Count(src, sep)+1)
	index := strings.Index(src, sep)
	n := len([]rune(sep))
	for index >= 0 {
		res = append(res, src[:index])
		src = src[index+n:]
		index = strings.Index(src, sep)
	}
	res = append(res, src)
	return res, nil
}

/* 斐波那契数列 */
func Fib(n int) int {
	if n < 2 {
		return n
	}
	a, b := 0, 1
	var c int
	for n > 0 {
		c = a + b
		a = b
		b = c
		n--
	}
	return a
}
